package com.budwk.app.sqfwV2.generator.lanhuadu.pdf;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BasePdfParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BaseTemplatePdfGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.component.ProblemComponent;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.enums.lanHuaDu.LanHuaDuFileServiceEnum;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 安全责任，版本号1.0
 */

@Slf4j
public class lhdSafetyHazardDocGenerator extends BaseTemplatePdfGenerator{

    /**
     * 当该场所上一次巡查存在限期整改问题时，且本次巡查该隐患问题仍然为“未整改”时，生成该文档。
     *  1 场所面积大于等于300
     *  2 上一次的数据存在期限整改 本次数据存在期限整改
     * @param pdfDTO
     * @return
     */
    @Override
    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        if (pdfDTO.isDownTemplate()) {
            return true;
        }
        FireSafetyOnDayingRoadPdfDTO roadPdfDTO = ProblemComponent.getSafetyPdfDTO(pdfDTO);
        // 初始数据 没有复查数据
        if (StringUtils.isEmpty(roadPdfDTO.getLastSummaryId())) {
            return false;
        }
        if(new BigDecimal(roadPdfDTO.getPlaceArea()).compareTo(new BigDecimal(300)) < 1){
            return false;
        }
        // 检查上次的数据是否存在期限整改
        SqfwFormsummaryNewService formsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        Sqfw_formSummary_new lastData = formsummaryNewService.fetch(roadPdfDTO.getLastSummaryId());
        FireSafetyOnDayingRoadPdfDTO lastPdfDto = JSONObject.parseObject(lastData.getParamData(), FireSafetyOnDayingRoadPdfDTO.class);
        Integer isRectified = lastPdfDto.getIsRectified();
        if (isRectified == 3 && roadPdfDTO.getIsRectified() == 3) {
            // 上一次的数据存在期限整改 本次数据存在期限整改
            return true;
        }
        return false;
    }

    @Override
    public String getTemplateName() {
        return LanHuaDuFileServiceEnum.B.getFilePath();
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, SqfwPdfCreateCommonBO pdfDTO) {
        if (pdfDTO.isDownTemplate()) {  // 下载模板
            buildNullData(paramMap);
            return;
        }
        FireSafetyOnDayingRoadPdfDTO roadPdfDTO = DayingRoadPdfComponent.getFireSafetyOnDayingRoadPdfDTO(paramMap, pdfDTO);
        BasePdfParamDTO basePdfParam = pdfDTO.getBasePdfParamDTO();
        paramMap.put("district", StrUtil.formatZhiShu(basePdfParam.getDistrict()));
        paramMap.put("county", StrUtil.formatZhiShu(basePdfParam.getCounty()));
        paramMap.put("address", StrUtil.formatStr(roadPdfDTO.getAddress()));
        // 隐患内容
        String existDangerStr = roadPdfDTO.getExistDangerStr();
        if (StringUtils.isNotEmpty(existDangerStr)) {
            existDangerStr = existDangerStr.replace("\n", "");
        }
        paramMap.put("existDangerStr", existDangerStr);
        paramMap.put("fillDate", DateUtil.format(basePdfParam.getFillDate(), DatePattern.CHINESE_DATE_PATTERN));
    }

    private void buildNullData(Map<String, Object> paramMap) {
        paramMap.put("district", "XX区");
        paramMap.put("county", "XX街道");
        paramMap.put("address", "XX地址");
        // 隐患内容
        paramMap.put("existDangerStr","");
        paramMap.put("fillDate", "202_年_月_日");
    }
}
